package com.ljs.clickhousedemo;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.builder.GeneratorBuilder;
import com.baomidou.mybatisplus.generator.config.querys.ClickHouseQuery;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

/**
 * @Description:clickhouse用mybatis-plus自动生成器
 * @Author: liujinsheng
 * @Date: 2021/10/26.
 */
public class CodeGenerator {
    static final String PROJECT_PATH = System.getProperty("user.dir");
    private static final String JAVA_PATH = "/src/main/java/com/ljs/generator";
    static final String AUTHOR = "ljs";
    public static void main(String[] args) {

        // 获取项目路径

        // 全局配置
        GlobalConfig gc = GeneratorBuilder.globalConfigBuilder()
                .openDir(true)
                //文件输出路径
                .outputDir(PROJECT_PATH+JAVA_PATH)
                .author(AUTHOR)
                .enableSwagger()
                .dateType(DateType.ONLY_DATE)
                .commentDate("yyyy-MM-dd").build();


        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig.Builder("jdbc:clickhouse://cc-wz9s6680uj4n3ma00.ads.rds.aliyuncs.com:8123", "bigdata", "y6GvlPjb4zEk")
                .dbQuery(new ClickHouseQuery()).build();


        // 包配置
        PackageConfig pc = GeneratorBuilder.packageConfigBuilder().parent("com.ljs.clickhousedemo").build();

        // 策略配置
        StrategyConfig strategy = GeneratorBuilder.strategyConfigBuilder()
                .addInclude("t_member_info")
                .addTablePrefix(pc.getModuleName() + "_")
                .controllerBuilder().enableHyphenStyle()
                .entityBuilder()
                .naming(NamingStrategy.underline_to_camel)
                .columnNaming(NamingStrategy.underline_to_camel)
                .versionColumnName("version").logicDeleteColumnName("isDelete")
                .enableLombok()
                .build();

        TemplateConfig templateConfig = GeneratorBuilder.templateConfigBuilder().build();

        // 代码生成器
        AutoGenerator mpg = new AutoGenerator(dsc).global(gc).strategy(strategy).template(templateConfig).packageInfo(pc);


        mpg.execute(new FreemarkerTemplateEngine());

    }
}
